package FanShe4;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

public class TestPerson {
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //获取反射对象
        Class<Person> personClass = Person.class;
        System.out.println("personClass = " + personClass);
        //实例化反射对象 访问
        Constructor<?>[] constructors = personClass.getConstructors();
        System.out.println("constructors = " + constructors);
        System.out.println("Arrays.toString(constructors) = " + Arrays.toString(constructors));
        //通过反射的对象访问构造器
        Constructor<Person> constructor = personClass.getConstructor(String.class, int.class);
        Person xiaowang = constructor.newInstance("小王", 20);
        Method sayHello = personClass.getMethod("sayHello");
        sayHello.invoke(xiaowang);
    }
}
